﻿using System;
using System.Drawing;
using System.IO;
using CosyTrade.Data;
using CosyTrade.Store;
using CosyTrade.UI;
using System.Windows.Forms;

namespace CosyTrade.ClientForm.Store
{
    public partial class ProductDetailForm : DetailFormBase
    {
        public ProductDetailForm() : this(Guid.Empty)
        {
        }

        public ProductDetailForm(Guid productKey) : base(productKey)
        {
            InitializeComponent();
        }

        protected override BusinessObject InitBusinessObject(Guid currObjKey)
        {
            return Operation.ProductManager.GetProductDetail(currObjKey);
        }

        protected override BusinessObject CreateNewBusinessObject()
        {
            return new Product();
        }

        protected override string GetTitle()
        {
            return "商品详情";
        }

        protected override void DataBinding()
        {
            BindingControl(txtBarcode, "EditValue", "BarCode");
            BindingControl(txtChinese, "EditValue", "CaptionChinese");
            BindingControl(txtSpanish, "EditValue", "CaptionSpaish");
            BindingControl(txtModule, "EditValue", "ModuleInfo");
            BindingControl(txtMaterial, "EditValue", "Material");
            BindingControl(txtGrossWeight, "EditValue", "GrossWeight");
            BindingControl(txtBulk, "EditValue", "Bulk");
            BindingControl(popboxProductCategory, "EditValue", "Category");
            BindingControl(txtMemo, "EditValue", "Memo");
            BindingControl(txtFactoryModule, "EditValue", "FactoryModule");
            BindingControl(txtProductCode, "EditValue", "ProductCode");

            Product pro = CurrentObject as Product;
            if (pro != null)
            {
                byte[] imgData = Operation.DataStorage.GetData(pro.ImagePath);
                if (imgData != null)
                    pictureBox.Image = new Bitmap(new MemoryStream(imgData));
            }
        }

        protected override bool IsVail()
        {
            if (Functions.ValueIsEmpty(txtChinese, "请输入商品中文名"))
                return false;
            if (Functions.ValueIsEmpty(txtSpanish, "请输入商品西班牙名"))
                return false;
            if (popboxProductCategory.EditValue == null)
            {
                Functions.ShowMessageBox("请选择商品所属分类");
                return false;
            }
            if (!Functions.ValueIsEmpty(txtGrossWeight))
            {
                double weight;
                if (!double.TryParse(Functions.ToDBC(txtGrossWeight.Text), out weight))
                {
                    Functions.ShowMessageBox("毛重只能输入数字");
                    txtGrossWeight.Focus();
                    return false;
                }
            }
            return true;
        }

        protected override void Save(BusinessObject currentObj)
        {
            if (!(currentObj is Product)) return;
            SaveImage();
            switch (CurrentStat)
            {
                case DetailFormCurrentStat.AddStat:
                    Operation.ProductManager.CreateProduct((Product) currentObj);
                    break;
                case DetailFormCurrentStat.EditStat:
                    Operation.ProductManager.UpdateProduct((Product) currentObj);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void SaveImage()
        {
            if (string.IsNullOrEmpty(btnChioeseImg.Tag as string)) return;

            Product pro = CurrentObject as Product;
            if (pro == null) return;

            string newImg = (string) btnChioeseImg.Tag;
            if (!File.Exists(newImg)) return;
            
            if(CurrentStat == DetailFormCurrentStat.EditStat)
            {
                string oldTag = pro.ImagePath;
                if (!string.IsNullOrEmpty(oldTag))
                    Operation.DataStorage.DeleteData(oldTag);
            }

            string saveKey = Operation.DataStorage.SaveData(Functions.GetMiniImage(newImg));
            pro.ImagePath = saveKey;
        }

        private void productCategoryDropdownControl1_ValueSelected(object sender, EventArgs e)
        {
            popboxProductCategory.EditValue = productCategoryDropdownControl1.SelectedProductCategory;
            popboxProductCategory.ClosePopup();
        }

        private void popboxProductCategory_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (e.Value as ProductCategoryBase != null)
                e.DisplayText = string.Format("{0} - {1}", ((ProductCategoryBase) e.Value).CategoryChinese,
                                              ((ProductCategoryBase) e.Value).CategoryChinese);
        }

        private void ProductDetailForm_Load(object sender, EventArgs e)
        {

        }

        private void btnChioeseImg_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            string imgPath = openFileDialog1.FileName;
            btnChioeseImg.Tag = imgPath;
            pictureBox.Image = Image.FromFile(imgPath);
        }
    }
}
